package com.jr.dao.impl;

import com.jr.dao.IBookInfoDao;
import com.jr.pojo.BookInfo;
import com.jr.util.DBHelper;
import com.jr.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class BookInfoDaoImpl implements IBookInfoDao {

    DBHelper db = new DBHelper();

    @Override //1.分页查询 总条数
    public int selectPageDataCount() {
        StringBuilder sql =new StringBuilder("select count(bookId) from bookinfo where 1=1");
        Connection con =null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            con = db.getCon();
            ps = con.prepareStatement(sql.toString());
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con,ps,resultSet);
        }
        return 0;
    }

    @Override //2.分页查询 查询当前页面 展示集合
    public ArrayList<BookInfo> selectIndexDate(PageHelper ph) {
        String sql="select * from bookinfo limit ?,?";
        int index = (ph.getPageIndex()-1)*ph.getShowNumber();
        return db.dtQuery(sql, BookInfo.class, index, ph.getShowNumber());
    }

}
